package com.xiaoye.qbproject.controller.qbwd;

import com.xiaoye.qbproject.controller.base.BaseController;
import com.xiaoye.qbproject.domain.AjaxResult;
import com.xiaoye.qbproject.service.QaService;
import com.xiaoye.qbproject.vo.QaSearchRes;
import com.xiaoye.qbproject.vo.QaVo;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/qa")
public class QaController extends BaseController {
    @Autowired
    private QaService qaService;

    /**
     * 获取搜索结果接口
     * 根据关键词获取相关的搜索结果，并返回成功响应
     *
     * @param keyword 搜索关键词
     * @return AjaxResult 包含搜索结果的AjaxResult对象
     */
    @GetMapping("/search")
    public AjaxResult getSearch(String keyword, @RequestParam(required = false, defaultValue = "10") Integer size) {
        List<QaSearchRes> search = qaService.getSearch(keyword);
        if (CollectionUtils.isEmpty(search)) {
            return AjaxResult.success(search);
        }
        // 只取10条
        return AjaxResult.success(search);
    }

    @PostMapping()
    public AjaxResult qa(@RequestBody QaVo qaVo, HttpServletRequest request) {
        String problem = qaVo.getProblem();
        if (problem == null || problem.isEmpty()) {
            return AjaxResult.error("问题不能为空");
        }
        String result = qaService.qa(qaVo, request);
        if (result == null) {
            return AjaxResult.error("您好,您的问题暂时无法回答!");
        }
        return AjaxResult.success(result);
    }
}
